<?php
// <editor-fold defaultstate="collapsed" desc="php">
include '../includes/constants.php';
include '../includes/usuario.php';


$usuario = new usuario();
$usuario->confirmar_miembro();
$exito = false;
if (isset($_GET['logout']) && $_GET['logout'] == true) {
    $usuario->log_Out();
}
if (isset($_GET['id']) || isset($_SESSION['usuario'])) {
    $id = intval(isset($_GET['id']) ? $_GET['id'] : $_SESSION['usuario']['id']);
    $registro = $usuario->ver($id);
    $datos = $registro;
    $roles = $usuario->dame_query("select * from rol");
    $roles = $roles['data'];
}
if (isset($_POST['enviar'])) {
    $data = $_POST;
    $data['imagen'] = $_FILES;
    unset($data['password2'], $data['enviar']);
    $exito = $usuario->editar($data, array("id" => $_POST['id']));
}
// </editor-fold>

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link href="../css/style.css" media="all" rel="stylesheet" type="text/css"/>
        <title>CCI Venezuela:: Editar Usuario</title>
        <script type="text/javascript" language="javascript" src="../js/jquery-1.4.2.min.js"></script>
        <script type="text/javascript" language="javascript" src="../js/index.js" ></script>
        <script type="text/javascript" language="javascrpt" src="../js/jquery-validate/jquery.validate.min.js"></script>
        <script type="text/javascript" src="../js/jquery-validate/localization/messages_es.js"></script>
        <script type="text/javascript" language="javascript">
            $(document).ready(function(){
                $("#content form").validate({
                    rules:{
                        foto:{accept:"jpg|png"},
                        nombre:{minlength:3},
                        apellido:{minlength:3},
                        login:{minlength:6,maxlength:20},
                        password:{minlength:6},
                        password2:{equalTo:"#password"}
                    }
                });
            });
        </script>
    </head>
    <body>
        <div id="header">
            <?php include '../template/header.php'; ?>
        </div>
        <div id="content">
            <div>
                <h1>Editar Usuario</h1>
                <div>
                    <?php if (!$exito && isset($registro) && count($registro) > 0): ?>
                        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/encode">
                            <fieldset>
                                <legend>Datos Personales</legend>
                                <div id="formData">
                                    <input type="hidden" name="id" value="<?php echo $datos['id']; ?>"/>
                                    <table width="100%" border="0" cellpadding="0" cellspacing="0">
                                        <tr>
                                            <td>
                                                <div class="c50">
                                                    <div>
                                                        <label for="nombre">Nombre<sup>*</sup></label>
                                                        <input type="text" name="nombre" class="required" value="<?php echo $datos['nombre']; ?>"/>
                                                    </div>
                                                    <div>
                                                        <label for="apellido">Apellido<sup>*</sup></label>
                                                        <input type="text" class="required" name="apellido" value="<?php echo $datos['apellido']; ?>"/>
                                                    </div>
                                                    <div>
                                                        <label for="login">Login<sup>*</sup></label>
                                                        <input type="text" class="required" name="login" value="<?php echo $datos['login']; ?>" disabled="disabled"/>
                                                    </div>
                                                    <div>
                                                        <label for="email">Email:<sup>*</sup></label>
                                                        <input type="text" class="required email" name="email" value="<?php echo $datos['email']; ?>"/>
                                                    </div>
                                                    <div>
                                                        <label for="password">Password<sup>*</sup></label>
                                                        <input type="password" name="password" id="password" class="required" value="<?php echo $datos['password']; ?>"/>
                                                    </div>
                                                    <div>
                                                        <label for="password2">Repita su password</label>
                                                        <input type="password" name="password2" id="password2" value="<?php echo $datos['password']; ?>"/>
                                                    </div>
                                                    <div>
                                                        <label for="telefono">Tel&eacute;fono<sup>*</sup></label>
                                                        <input type="text" name="telefono" class="required" value="<?php echo $datos['telefono']; ?>"/>
                                                    </div>
                                                    <div>
                                                        <label for="telefono2">Tel&eacute;fono 2</label>
                                                        <input type="text" name="telefono2" value="<?php echo $datos['telefono2']; ?>"/>
                                                    </div>
                                                </div><!-- fin columna uno -->
                                            </td>
                                            <td>
                                                <div class="c50">
                                                    <div>
                                                        <label for="foto">Foto</label>
                                                        <input type="file" name="foto"/>
                                                        <?php if (!empty($datos['foto'])): ?>
                                                            <div>
                                                                <img src="<?php echo ROOT . IMAGES_USUARIOS . $datos['foto']; ?>" style="border-radius:5px; width: 100px; margin: 5px; "/>
                                                            </div>
                                                        <?php endif; ?>
                                                    </div>
                                                    <?php if ($_SESSION['usuario']['rol'] == 2): ?>
                                                        <div>
                                                            <label for="rol">Rol</label>
                                                            <select name="rol">
                                                                <?php foreach ($roles as $rol): ?>
                                                                    <option value="<?php echo $rol['id'] ?>" <?php echo ($rol['id'] == $datos['rol']) ? "selected='selected'" : ""; ?>><?php echo $rol['nombre']; ?></option>
                                                                <?php endforeach; ?>
                                                            </select>
                                                        </div>
                                                        <div>
                                                            <label for="status">Status</label>
                                                            <div><br/>
                                                                <span>Activo</span><input type="radio" name="status" value="1" <?php echo ($datos['status'] == 1) ? "CHECKED ='CHECKED '" : "" ?>/>
                                                                <span>Inactivo</span><input type="radio" name="status" value="0" <?php echo ($datos['status'] == 0) ? "CHECKED ='CHECKED '" : "" ?>/>
                                                            </div>
                                                        </div>
                                                    <?php endif; ?>
                                                </div> <!-- columna 2 --></td>
                                        </tr>
                                    </table>
                                    <div class="clearBoth">
                                        <center>
                                            <input type="submit" name="enviar" value="Editar"/>
                                        </center>
                                    </div>
                                </div>
                            </fieldset>
                        </form>
                    <?php elseif ($exito): ?>
                        <div class="info">
                            <p>Usuario editado con &eacute;xito.</p>
                        </div>
                    <?php else: ?>
                        <div class="info">
                            <p>Hubo un problema al conectar con la base de datos. Por favor intente de nuevo o contacte con el Administrador del sistema.</p>
                        </div>
                    <?php endif; ?>
                </div>
                <div align="center">
                    <a href="index.php">Volver al men&uacute;</a>
                </div>
            </div>
        </div>
        <div id="footer">
            <?php include '../template/footer.php'; ?>
        </div>
    </body>
</html>
